<?php
/**
 * User: hjd-yao
 * Date: 2018/11/6
 * Time: 16:54
 */

namespace App\Models\System;


use App\Models\BaseModel;

class AuthModel extends BaseModel
{
    protected $table = 'sys_auth';
    public $timestamps = false;

    public function getAllActionsTitle()
    {
        return self::where('status',1)->pluck('title','id')->toArray();
    }
    public function getTreesActions()
    {
        $list = self::where('status',1)->select('id','parent_id','title')->get()->toArray();
        $default =  [['value'=>'0','label'=>'顶级功能']];
        if($list){
            $default = array_merge($default,format_cascade($list));
        }
        return $default;
    }

    public function getDTreeActions($is_show = false)
    {
        $list = self::where('status',1)->select('id','name','title','parent_id','parent_ids')->get()->toArray();
        if($list){
            $new_list = [];
            foreach ($list as $key=>$item){
                $new_list[$key] = $item;
                $new_list[$key]['parentId'] = $item['parent_id'];
                $new_list[$key]['isLast'] = in_array($item['id'],array_column($list,'parent_id'))?false:true;
                $new_list[$key]['level'] = count(str2arr($item['parent_ids']))+1;
                $new_list[$key]['checkArr'] = [['type'=>0,'isChecked'=>0]];
                if($is_show){
                    $new_list[$key]['basicData'] = [
                        'nodeName'=>$item['name'],
                        'editUrl'=>route('d_Action_editAction',['action_id'=>$item['id']]),
                        'deleteUrl'=>route('d_Action_delete',['action_id'=>$item['id']]),
                    ];
                }
                unset($new_list[$key]['parent_id'],$new_list[$key]['parent_ids']);
            }
            $list = list_to_tree($new_list,true,'id','parentId','children');
        }

        return $list;
    }
}